query=[];
function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
var gallery = function(width,height)
                        {
                            //var self=this;
                            if (isNumber(width)) this.width=width;
                            if (isNumber(height)) this.height=height;
                            var grid_width=this.width*160+10;
                            var grid_height=this.height*210+10;
                            $('#grid').css({'width':grid_width, 'height':grid_height});
                            var grid_position=$('#grid').offset();
                            $('#prev').css({'left': grid_position.left-125});
                            $('#next').css({'left': grid_position.left+grid_width+5});
                            //$('#prev').click(this.prevPage());
                            //$('#next').click(this.nextPage());
                        };
//gallery.prototype.constructor
gallery.prototype.width=3;
gallery.prototype.height=4;
gallery.prototype.query=[];
gallery.prototype.cur_page=1;
gallery.prototype.nextPage =
    function ()
    {
        this.getPage(this.cur_page+1);
    };
gallery.prototype.prevPage =
    function ()
    {
        this.getPage(this.cur_page-1);
    };
gallery.prototype.getPage =
    function (number)
    {
        //this.query=[];
        query=[];
        var self=this;
        $('#grid').css({'opacity': '0'});
        $('#grid').animate({opacity: 1}, 2000);
        $('#grid').html('<span class="anchor"></span>');
        this.cur_page=number;
        if (this.cur_page-1==0)
        {
            $("#prev").hide();
        }
        else
        {
            $("#prev").show();
        }
        $.post(
            '/ajax/getpage',
            {
                page:this.cur_page,
                grid_length:this.width*this.height
            },
            function (data)
            {
                //alert(data);
                max_pages=data.pages;
                if (number==max_pages)
                {
                    $("#next").hide();
                }
                else
                {
                    $("#next").show();
                }

                $.each(data.data,
                    function(index,value)
                    {
                        $('.anchor').before('<div><img src="/public/css/img/loading.gif" data-load_id="'+value.id+'"><img onclick="show_image('+value.id+')" src="'+value.path_to_thumb+'" data-id="'+value.id+'" style="display:none" ></div>');
                        $('#grid div img:eq(1)').on('load',self.addToQuery(value.id));
                    }
                );

            }

        );
        check=setInterval(this.isQueryFull,1000);
    };
gallery.prototype.isQueryFull=
    function()
    {
        if (query.length==$('#grid div').size())
        {
            clearInterval(check);
            timer=setInterval(randomFadeIn, 150);
            if (query.length<1) clearInterval(timer);
           // this.startShow();
        }
    };
/*gallery.prototype.startShow =
    function ()
    {
        timer=setInterval(this.randomFadeIn, 150);
        if (query.length<1) clearInterval(timer);
    }; */
gallery.prototype.randomFadeIn=
    function()
    {
        var rnd_max=query.length;
        var rnd_num=Math.floor(Math.random()*(rnd_max+1));
        var id=query[rnd_num];
        $('img[data-load_id="'+id+'"]').hide();
        $('img[data-id="'+id+'"]').fadeIn(400);
        query.splice(rnd_num,1);
    };
gallery.prototype.showImage=
    function (id)
    {
        $.post(
            '/ajax/getimageinfo',
            {
                id:id
            },
            function (data)
            {

                $.colorbox(
                    {
                        href:data.path_to_fullsize,
                        title:data.description
                    }
                );
            }
        );
    };
gallery.prototype.addToQuery=
    function (id)
    {
        query.push(id);
    };

_gallery=new gallery();

function show_image(id)
{
    _gallery.showImage(id);
}

function randomFadeIn()
{
    _gallery.randomFadeIn();
}
function chkForm()
{
    if ($('#add_image input[type="file"]').val() == '') {
        alert('Вы не выбрали изображение!');
    }
    else {
        $('form').submit();
    }
}

$( function ()
    {
        _gallery.getPage(_gallery.cur_page);
        $("#next").attr('onclick', '_gallery.nextPage()');
        $("#prev").attr('onclick', '_gallery.prevPage()');
    }
);

